﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace MoveFiles
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length != 2)
            {
                Console.WriteLine("Two string parameters expected!");
                return;
            }

            string fromPath = args[0];
            string fileMask = "*.*";
            if (fromPath.Contains("\\*."))
            {
                int index = fromPath.IndexOf("\\*.");
                fileMask = fromPath.Substring(index + 1, fromPath.Length - index - 1);
                fromPath = fromPath.Substring(0, index);
            }
            
            if (!Directory.Exists(fromPath))
            {
                Console.WriteLine("Source dir not exists!");
                return;
            }

            string toPath = args[1];
            if (!Directory.Exists(toPath))
            {
                Console.WriteLine("Dest dir not exists!");
                return;
            }

            DateTime endDate = DateTime.Today;
            DateTime begDate = endDate.AddDays(-1);

            DirectoryInfo info = new DirectoryInfo(fromPath);
            List<FileInfo> files = info.GetFiles(fileMask, SearchOption.AllDirectories).ToList();
            foreach (FileInfo file in files)
            {
                try
                {
                    DateTime fileDate = File.GetLastWriteTime(file.FullName);
                    if ((fileDate < begDate) || (fileDate >= endDate))
                        continue;

                    string toFileName = file.FullName.Replace(fromPath, toPath);
                    string toFileDir = Path.GetDirectoryName(toFileName);
                    if (!Directory.Exists(toFileDir))
                        Directory.CreateDirectory(toFileDir);
                    File.Move(file.FullName, toFileName);
                }
                catch (Exception e)
                {
                }
            }
        }
    }
}
